package cn.zifangsky.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Autowired
    private Environment env;

    @Bean(value = "restApi")
    public Docket restApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("基础信息")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any()).build();
    }


    // @Bean
    // public Docket createRestApi2() {
    //     return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
    //             .apis(RequestHandlerSelectors.basePackage("com.watertek.rest.controller"))
    //             .paths(PathSelectors.any()).build();
    // }


    private ApiInfo apiInfo() {
        String ip = "127.0.0.1";
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            ip = ip4.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String port = env.getProperty("server.port");
        return new ApiInfoBuilder()
                .title("111")
                .description("接口可视化")
                .contact(new Contact("111", "https://" + ip + ":" + port, "111com"))
                .termsOfServiceUrl("https://" + ip + ":" + port + "/doc.html")
                .version("1.0")
                .build();
    }
}
